<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/24
 * Time: 11:02
 */

namespace app\shop\admin;

use app\admin\controller\Admin;
use app\shop\validate\BannerValidate;
use app\common\builder\ZBuilder;
use app\shop\model\BannerModel;
use app\shop\model\GoodsModel;

class Ad extends Admin
{
    public function banner()
    {
        $btn_add = [
            'title' => '添加',
            'icon' => 'fa fa-plus',
            'href' => url('banneradd')
        ];
        $btn_edit = [
            'title' => '编辑',
            'icon' => 'fa fa-pencil',
            'href' => url('banneredit',['id' =>'__id__'])
        ];
        $list = BannerModel::getList();
        return ZBuilder::make('table')
            ->setPageTitle('轮播图管理')
            ->setTableName('banner')
            ->addColumns([
                ['id','ID'],
                ['img','图片','picture'],
                ['desc','描述'],
                ['right_button','操作']
            ])
            ->addTopButton('add',$btn_add)
            ->addRightButton('edit', $btn_edit)
            ->addRightButton('delete')
            ->setRowList($list)
            ->fetch();
    }

    public function banneradd()
    {
        if(request()->isPost()){
            $this->bannerStore();
        }
        $select_options = [
            '0' => '指定商品',
            '1' => '外部链接',
        ];
       $js = $this->setJs();
        $goods_list = $this->getGoodsList();
        return ZBuilder::make('form')
            ->setPageTitle('添加轮播图')
            ->addFormItems([
                ['text','desc', '描述'],
                ['image','img','图片'],
                ['select','link_type', '链接类型', '', $select_options, '0'],
                ['select', 'goods_id', '链接商品','',$goods_list],
                ['text', 'out_link', '外部链接'],
                ['text', 'out_link_img', '外链图片']
            ])
            ->setUrl(url('banneradd'))
            ->setExtraJs($js)
            ->fetch();
    }

    public function banneredit($id = 0)
    {
        if(request()->isPost()){
            $this->bannerStore();
        }
        $select_options = [
            '0' => '指定商品',
            '1' => '外部链接',
        ];
        $js = $this->setJs();
        $goods_list = $this->getGoodsList();
        $banner_item = BannerModel::getOne($id);
        return ZBuilder::make('form')
            ->setPageTitle('添加轮播图')
            ->addFormItems([
                ['hidden', 'id'],
                ['text','desc', '描述'],
                ['image','img','图片'],
                ['select','link_type', '链接类型', '', $select_options, '0'],
                ['select', 'goods_id', '链接商品','',$goods_list],
                ['text', 'out_link', '外部链接'],
                ['text', 'out_link_img', '外链图片']
            ])
            ->setUrl(url('banneredit'))
            ->setExtraJs($js)
            ->setFormData($banner_item)
            ->fetch();
    }

    private function getGoodsList()
    {
        $goods_list = GoodsModel::getList();
        $goods_list2 = [];
        foreach ($goods_list as $item){
            $goods_list2[$item['id']] = $item['name'];
        }
        return $goods_list2;
    }

    private function setJs()
    {
        $js = <<<EOF
    <script>
        $(function() {
          hideAll();
          $("#form_group_goods_id").show();
          $("#link_type").on('change', function() {
            var val = $(this).val();
             hideAll();
            if(val == 0){
                $("#form_group_goods_id").show();
            }else if(val == 1){
                $("#form_group_out_link").show();
                $("#form_group_out_link_img").show();
            }
          })
        });
        
        function hideAll() {
          $("#form_group_goods_id").hide();
          $("#form_group_out_link").hide();
          $("#form_group_out_link_img").hide();
        }
    </script>
EOF;
        return $js;
    }

    private function bannerStore()
    {
        $data = input('post.');
        $id = input('param.id', 0);
        $validate = new BannerValidate();
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }
        $res = BannerModel::store($data, $id);
        if($res){
            $this->success('保存成功', 'banner');
        }else{
            $this->error('保存失败');
        }
    }
}